library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(xlsx)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
##
## select
bball = read.xlsx("baseball-2016.xlsx", sheetName = "Sheet1", header = TRUE,
row.names = 1)
head(bball)
## League Won Lost Runs.per.game HR.per.game AB Runs
## Aizona Diamondbacks NL 69 93 4.64 1.172840 5665 752
## Atlanta Braves NL 68 93 4.03 0.757764 5514 649
## Baltimore Orioles AL 89 73 4.59 1.561728 5524 744
## Boston Red Sox AL 93 69 5.42 1.283951 5670 878
## Chicago Cubs NL 103 58 4.99 1.236025 5503 808
## Chicago White Sox AL 78 84 4.23 1.037037 5550 686
## Hits X2B X3B HR RBI StolenB CaughtS BB SO BAvg
## Aizona Diamondbacks 1479 285 56 190 709 137 31 463 1427 0.261
## Atlanta Braves 1404 295 27 122 615 75 34 502 1240 0.255
## Baltimore Orioles 1413 265 6 253 710 19 13 468 1324 0.256
## Boston Red Sox 1598 343 25 208 836 83 24 558 1160 0.282
## Chicago Cubs 1409 293 30 199 767 66 34 656 1339 0.256
## Chicago White Sox 1428 277 33 168 656 77 36 455 1285 0.257
## OBP SLG OPS TB GDP HBP SH SF IBB LOB
## Aizona Diamondbacks 0.320 0.432 0.752 2446 117 50 43 38 43 1113
## Atlanta Braves 0.321 0.384 0.705 2119 145 59 64 52 60 1161
## Baltimore Orioles 0.317 0.443 0.760 2449 119 44 17 36 19 1065
## Boston Red Sox 0.348 0.461 0.810 2615 137 43 8 40 34 1162
## Chicago Cubs 0.343 0.429 0.772 2359 107 96 42 37 45 1217
## Chicago White Sox 0.317 0.410 0.727 2275 122 53 29 44 16 1105
#Q2
bball.numeric = bball[,3:27]
distance = dist(bball.numeric)
res = isoMDS(distance, k=2)
## initial value 12.033362
## final value 12.032500
## converged
coords = res$points
coordsMDS = as.data.frame(coords)
coordsMDS$name = rownames(coordsMDS)
coordsMDS$league = bball$League
plot_ly(coordsMDS, x=~V1, y=~V2, type="scatter", mode = "markers"
, hovertext=~name, color= ~league)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
#Q3
sh <- Shepard(distance, coords)
delta <-as.numeric(distance)
D<- as.numeric(dist(coords))
n=nrow(coords)
index=matrix(1:n, nrow=n, ncol=n)
index1=as.numeric(index[lower.tri(index)])
n=nrow(coords)
index=matrix(1:n, nrow=n, ncol=n, byrow = T)
index2=as.numeric(index[lower.tri(index)])
plot_ly()%>%
add_markers(x=~delta, y=~D, hoverinfo = 'text',
text = ~paste('Obj1: ', rownames(bball)[index1],
'<br> Obj 2: ', rownames(bball)[index2]))%>%
add_lines(x=~sh$x, y=~sh$yf)
#Q4
po = plot_ly(x=~coordsMDS$V1, y=~bball[, 4], type="scatter", mode = "markers"
, hovertext=~rownames(bball))
print(po)
cols_bball = colnames(bball)
dim(bball)[2]
## [1] 27
plots_bball = list()
for(i in 2:4){
pl_name = paste("P", i)
plots_bball[[pl_name]] = plot_ly(x=~coordsMDS$V1, y=~bball[,i], type="scatter", mode = "markers"
, hovertext=~rownames(bball))
}
invisible(lapply(plots_bball, print))